<template>
  <h3>用户管理</h3>
  <el-divider />
  <div class="opt-div">
    <div>
    </div>
    <!-- <el-pagination
      v-model:current-page="pages.currentPage"
      v-model:page-size="pages.pageSize"
      :page-sizes="[5,10, 20, 50]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="bookData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />  -->
    <el-table :data="userData.list">
      <el-table-column width="80px"  type="index" />
      <el-table-column prop="username" label="登录名" />
      <el-table-column prop="realName" label="真实名称" />
      <el-table-column prop="phone" label="手机号" />
      <el-table-column prop="email" label="邮箱" />
       <el-table-column width="100px" label="角色" >
      <template #default="scope">
        <el-tag class="mx-1"  :type="scope.row.urole==0?'success':'primary'"
            >{{scope.row.urole==0?'管理员':'普通用户'}}</el-tag
          >
      </template>
    </el-table-column> 

      <el-table-column label="状态" width="100px">
        <template #default="scope">
          <el-tag class="mx-1" v-if="scope.row.ustate != 0" type="danger"
            >删除</el-tag
          >
          <el-tag v-else class="mx-1" type="success">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180px" />
      <el-table-column prop="updateTime" label="更新时间" width="180px" />
      <el-table-column label="操作" width="220px">
        <template #default="scope">
          <el-button
            type="success"
            size="small"
            @click="btn_re_password(scope.row)"
            >重置密码</el-button
          >
          <el-button
            type="warning"
            size="small"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            >禁用</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import http from "../axios";
import { ElMessage, ElMessageBox } from "element-plus";

const userData = ref([]);
const pages = ref({
  pageNum: 1,
  pageSize: 10,
});

onMounted(() => {
  user_list();
});

const user_list = () => {
  http
    .get("/user/list", {
      params: {
        ...pages.value,
      },
    })
    .then((resp) => {
      userData.value = resp.data.data;
    });
};

const btn_re_password = (p_user)=>{
  console.log(p_user);
  ElMessageBox.confirm(`确认重置 ${p_user.realName} 的密码吗？`
  ,'重置密码提示',{type:'warning'})
  .then(() => {
    http.put('/user/reset',p_user).then(resp=>{
      if(resp.data.code != '200'){
        ElMessage.error(resp.data.message);
        return;
      }
      ElMessage.success(`${resp.data.message},邮件已发送至${p_user.email}邮箱`);
    })
    
  })
}
</script>

<style scoped></style>
